package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.HashMap;

/**
 * https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/?envType=study-plan-v2&envId=top-100-liked
 * 滑动窗口
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-25 20:34
 */
public class Test08 {
    public static void main(String[] args) {
//        System.out.println(lengthOfLongestSubstring("aaajdjf a ajlkjal  kljalkjlkdjalks fa jfal   aljfla "));
        System.out.println(lengthOfLongestSubstring("aab"));
    }

    public static int lengthOfLongestSubstring(String s) {
        if (s.length() == 1 || s.length() == 0) {
            return s.length();
        }
        int win_len = 1;
        int point = 0;
        char[] chars = s.toCharArray();
        while (point + win_len <= s.length()) {
            while (check(chars, point, point + win_len - 1)) {//确实是无重复
                win_len++;
            }
            //有重复
            point++;
        }
        return win_len - 1;
    }

    public static Boolean check(char[] chars, int start, int end) {
        if (end >= chars.length) {
            return false;
        }
        HashMap<Character, Integer> hashMap = new HashMap<>();
        for (int i = start; i <= end; i++) {
            if (!hashMap.containsKey(chars[i])) {
                hashMap.put(chars[i], 1);
            } else {
                return false;
            }
        }
        // 无重复 true
        // 有重复 false
        return true;
    }

}
